<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\PlayBeads as PlayBeadsModel;

/**
 * PlayBeads 念珠控制器
 * Class PlayBeads
 * @package app\home\controller
 */
class PlayBeads extends Base
{
    /**
     * [getIndex  获取次数]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new PlayBeadsModel;
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        try{
            $res = $model->findData($mem['id'],'mid','id,total,today,update_time,insist_day');
            if(empty($res)){
                $res['total'] = 0;
                $res['today'] = 0;
                $res['insist_day'] = 0;
                return admin_json(1, '获取成功',$res);
            }
            if(strtotime('today') > strtotime($res['update_time'])){
                $res['today'] = 0;
            }
//            $last_time = strtotime('today') - strtotime(date('Y-m-d',strtotime($res['update_time'])));
//            if($last_time > 3600*24){
//                $res['insist_day'] = 0;
//                $model->editData(['id'=>$res['id'],'insist_day'=>0]);
//            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$res);
    }
    /**
     * [getAdd  敲击念珠]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $model = new PlayBeadsModel;
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        try{

            $res = $model->findData($mem['id'],'mid','id,total,today,update_time,insist_day');

            if(empty($res)){
                $data = array(
                    'mid'=>$mem['id'],
                    'total'=>$param['num'],
                    'today'=>$param['num'],
                    'insist_day'=>1
                );
                $model->addData($data);
            }else{
                $last_time = strtotime('today') - strtotime(date('Y-m-d',strtotime($res['update_time'])));
                if($last_time != 0){
                    $num = $param['num'];
                    $res['insist_day'] = $res['insist_day'] + 1;
                }else{
                    $num = $res['today'] + $param['num'];
                }
                $data = array(
                    'total'=>$res['total'] + $param['num'],
                    'today'=>$num,
                    'id'=>$res['id'],
                    'insist_day'=>$res['insist_day']
                );
                $model->editData($data);
            }
            add_quality($mem['id'],15,$mem['quality'],2);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
}